package com.jiai.cloud.community.controller;

import com.jiai.cloud.base.exception.ServiceException;
import com.jiai.cloud.base.tools.RestResultGenerator;
import com.jiai.cloud.base.util.SpringUtil;
import com.jiai.cloud.community.entity.TCatalog;
import com.jiai.cloud.community.entity.TResource;
import com.jiai.cloud.community.entity.Traverse;
import com.jiai.cloud.community.service.ITCatalogService;
import com.jiai.cloud.community.service.ITResourceService;
import com.jiai.cloud.config.Config;
import freemarker.cache.StrongCacheStorage;
import org.apache.commons.collections.map.HashedMap;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/6/23.
 */
@RestController
@RequestMapping("/api/v1/TResource")
public class TResourceController {

    @Autowired
    private ITResourceService itResourceService;


    @Autowired
    private ITCatalogService itCatalogService;
    //查所有
    @RequestMapping(value = "/findByAll",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"} )
    public Object findByAll(TResource tResource) throws ServiceException {
        try {
            List<TResource> list=itResourceService.selectResourMap(tResource);
            List<Traverse> list1=new ArrayList<Traverse>();
                    for(int i = 0 ; i < list.size() ; i++){
                        Traverse travers=new Traverse();
                        for (int j = 0;j < list.get(i).gettCatalogs().size();j++){
                            if ( list.get(i).gettCatalogs().get(j).getSaveCatalog()==null){
                                travers.setId(list.get(i).getId());
                                travers.setImgUrl( "http://localhost:8082/api/v1/file/img?fileName="+list.get(i).getUserId()+"/"+list.get(i).getImgUrl());
                            }else {
                                travers.setId(list.get(i).getId());
                                travers.setImgUrl("http://localhost:8082/api/v1/file/img?fileName="+list.get(i).getUserId()+"/"+ list.get(i).gettCatalogs().get(j).getSaveCatalog()+"/"+list.get(i).getImgUrl());
                        }
                        }
                        list1.add(travers);
                    }
                return RestResultGenerator.genSuccessResult(list1);
        }catch (Exception e) {
            throw new ServiceException("查询失败", e.getMessage());
        }
    }
    //单个删除文件即对应的数据库的数据
    @RequestMapping(value="/delete", produces = {"application/json;charset=UTF-8"},method = RequestMethod.POST)
    public Object delete(Integer id) throws ServiceException {
        try {
            String filePath = SpringUtil.getBean(Config.class).getFilePath();
            TResource tResource =itResourceService.selectById(id);
            String pata="";
            if (tResource.getCatalogId()==0){
                pata=filePath+"\\"+tResource.getUserId()+"\\"+tResource.getImgUrl();
            }else{
                TCatalog tCatalog =itCatalogService.selectById(tResource.getCatalogId());
                pata=filePath+"\\"+tResource.getUserId()+"\\"+tCatalog.getSaveCatalog()+"\\"+tResource.getImgUrl();
            }
            File  file = new File(pata);
            if (file.isFile() && file.exists()){
                itResourceService.deleteById(id);
                file.delete();
                return RestResultGenerator.genSuccessResult(id);
            }else{
                return RestResultGenerator.genErrorResult("文件删除失败");
            }
        }catch (Exception e){
            throw new ServiceException("删除失敗",e.getMessage());
        }
    }
}
